package com.zhongyouyun.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import com.zhongyouyun.admin.model.MenuModel;
import com.zhongyouyun.admin.model.UserModel;
import com.zhongyouyun.other.annotation.XianMuAuthScope;
import com.zhongyouyun.other.model.Result;
import com.zhongyouyun.utils.ModelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhongyouyun.admin.service.MenuService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author: shenyi
 * @Date: 2021-09-23
 * @Description: 资源表
 */
@RestController
@RequestMapping("/admin/z_menu")
@Api(value = "菜单管理controller", tags = {"菜单管理接口"})
public class MenuController {
    @Autowired
    private MenuService service;

    @GetMapping("/findMenuTreeList")
    @ResponseBody
    @ApiOperation(value = "获取当前所以菜单列表", notes = "所有菜单列表")
    @XianMuAuthScope(deptAlias = "temp_data_scope.")
    public Result<List<MenuModel>> findRoleMenuTree() {
        List<MenuModel> list = service.findMenuTreeList();
        if (list.size() > 0) {
            return Result.<List<MenuModel>>builder().message("成功").status(0).data(list).build();
        } else {
            return Result.<List<MenuModel>>builder().status(0).build();
        }
    }

    @PostMapping("/saveOrUpate")
    @ResponseBody
    @ApiOperation(value = "修改或者新增", notes = "通过ID判断修改或者新增")
    public Result saveOrUpate(@RequestBody MenuModel menuModel, HttpServletRequest request) {

        String type = request.getHeader("xiangmuType");
        UserModel model = (UserModel) SecurityUtils.getSubject().getPrincipal(); // 获取当前登录用户
        if (ObjectUtil.isNotEmpty(model.getId()) && ObjectUtil.isNotEmpty(menuModel.getId())) {
            menuModel.setUpdateBy(model.getId().toString());
        } else {
            menuModel.setXiangMuType(type);
            ModelUtils modelutils = new ModelUtils();
            modelutils.initModel(menuModel, model.getId().toString());
        }
        Boolean flag = service.saveOrUpdate(menuModel);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("保存失败").success(false).status(1).build();
        }
    }

    @GetMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除", notes = "通过ID删除")
    public Result delete(@ApiParam(name = "菜单ID", value = "id", required = true) @RequestParam(value = "id") String id) {
        Boolean flag = false;
        flag = service.removeById(Long.valueOf(id));
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("删除失败").success(false).status(1).build();
        }
    }

    @PostMapping("/batchDelete")
    @ResponseBody
    @ApiOperation(value = "批量删除", notes = "通过ID删除")
    public Result batchDelete(@RequestBody List<String> ids) {
        List<Long> list = new ArrayList<Long>();
        ids.forEach(id -> {
            list.add(Long.valueOf(id));
        });
        Boolean flag = service.removeByIds(list);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("删除失败").success(false).status(1).build();
        }
    }
}
